<template>
  <div class="index">
    <el-container>
      <el-header height="68px">
        <m-header></m-header>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <m-aside></m-aside>
        </el-aside>
        <el-main>
          <div class="purchase">
            <div class="title">在线购买</div>
            <div class="content">
              <div class="block-hd">
                <h2>闪信服务购买</h2>
              </div>
              <div class="rule">1、闪信均支持三网（电信、联通、移动）
                <br>2、无使用时间限制,余额永久有效
                <br>3、支持API接口发送
                <br>4、禁止政治、博彩、色情、诈骗等非法内容,详见
                <a
                  href="http://www.scio.gov.cn/xwfbh/xwbfbh/wqfbh/2015/33885/xgzc33891/Document/1458497/1458497.htm"
                  target="_blank"
                >《互联网信息服务管理办法》</a> 第十五条
              </div>
              <div class="product-line">
                <h6 class="titles">请选择需要购买的套餐</h6>
                <el-row :gutter="20">
                  <el-col :span="4" v-for="(item,index) in listData" :key="index">
                    <div
                      class="grid-content bg-purple"
                      :data-id="item.productID"
                      :data-price="item.productPrice"
                      ref="dataAttr"
                      @click="getData($event,index)"
                    >{{item.productName}}</div>
                  </el-col>
                </el-row>
                <h6 class="titles">请选择需要购买的套餐包数量</h6>
                <template>
                  <el-input-number
                    v-model="num"
                    @change="handleChange"
                    :min="1"
                    :max="100"
                    label="描述文字"
                  ></el-input-number>
                </template>
                <div class="block">
                  <div class="block-hd">合计</div>
                  <div class="block-content sub-block">
                    <ul class="sub-line no-bullet">
                      <li>
                        <span class="res">{{text}}</span>
                        购买的商品
                      </li>
                      <li>
                        <span class="res">{{num}}</span>
                        购买的数量
                      </li>
                    </ul>
                    <div class="sub-total">
                      <span class="price-total">{{total | myFilter}}</span>
                      小计
                    </div>
                    <div class="row">
                      <el-button type="primary" size="small" @click="submit">立即购买</el-button>
                    </div>
                  </div>
                </div>
              </div>
              <div class="payment-description">
                <hr>
                <div class="title1">关于支付的几点说明</div>
                <div class="content1">1. 如需通过对公账户进行打款，
                  <span class="blue" @click="linkIndex">请联系售后客服确认</span>
                  <br>2. 暂时不支持信用卡
                  <br>3. 由于微信支付相关限制，微信支付仅支持3000元以内的订单
                  <br>4. 以上商品价格均为含票价格，可在购买后
                  <span class="blue" @click="linkIndex">联系客服申请发票</span> 。
                </div>
              </div>
            </div>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { getCookie } from "./until/index";
export default {
  data() {
    return {
      num: 1,
      listData: [],
      text: "-",
      dataId: "",
      dataPrice: "0",
      total: 0
    };
  },
  created() {
    this.$ajax
      .get(
        "getSmsProductList.ashx?userId=" +
          getCookie("userid") +
          "&token=" +
          getCookie("token")
      )
      .then(res => {
        if (res.data.code === "200") {
          var data = res.data.data.productList;
          this.listData = data;
        } else {
          this.$message.error(res.data.msg + "请退出后重新登录");
        }
      })
      .catch(error => {
        console.log(error);
      });
  },
  filters: {
    myFilter: function(value) {
      return (value = "¥" + value);
    }
  },
  methods: {
    // 跳转联系客服
    linkIndex() {
      this.$router.push({
        path: "/index"
      });
    },
    // 计数
    handleChange(value) {
      this.num = value;
      this.total = (this.num * this.dataPrice).toFixed(2);
    },
    // 提交定单
    submit(res) {
      if (this.total) {
        this.$ajax
          .get(
            "saveSmsProductOrder.ashx?userId=" +
              getCookie("userid") +
              "&token=" +
              getCookie("token") +
              "&productID=" +
              this.dataId +
              "&productQty=" +
              this.num
          )
          .then(res => {
            if (res.data.code == 200) {
              this.$router.push({
                path: "/order-from?orderID=" + res.data.msg
              });
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch(error => {
            console.log(error);
          });
      } else {
        this.$message.warning("请选择购买套餐");
      }
    },
    // 获取购买套餐
    getData(e, index) {
      this.dataId = this.$refs.dataAttr[index].dataset.id;
      this.dataPrice = this.$refs.dataAttr[index].dataset.price;
      this.text = this.$refs.dataAttr[index].innerText;
      this.total = (this.num * this.dataPrice).toFixed(2);
      for (var i = 0, iL = this.$refs.dataAttr.length; i < iL; i++) {
        this.$refs.dataAttr[i].classList.remove("active");
      }
      this.$refs.dataAttr[index].classList.add("active");
    }
  }
};
</script>
<style scoped>
.index {
  overflow: hidden;
}
.el-header {
  background-color: #3a4759;
  color: #fff;
}
.el-aside {
  background-color: #485668;
  color: #333;
  text-align: center;
  line-height: 200px;
  overflow: hidden;
}
.el-main {
  background-color: #f0eff5;
  color: #333;
  text-align: center;
  height: 100%;
}
.blue {
  color: #276bff;
  cursor: pointer;
}
.purchase {
  padding-bottom: 30px;
  text-align: left;
}
.purchase .title {
  font-size: 30px;
  font-weight: 500;
  text-align: center;
  margin-bottom: 20px;
}
.content {
  width: 100%;
  background: #fff;
  padding: 20px;
  display: table;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  margin-bottom: 20px;
}
.rule {
  border-style: solid;
  border-width: 1px;
  border-color: #d8d8d8;
  margin-bottom: 10px;
  padding: 15px;
  background: #f5f8ff;
  color: #333;
  text-align: left;
  font-size: 14px;
  border-radius: 3px;
}
.block-hd {
  width: 100%;
}
.block-hd h2 {
  display: block;
  font-size: 14px;
  font-weight: bold;
  line-height: 30px;
  margin: 0;
  padding: 5px 0 4px 0;
  text-transform: uppercase;
}
.product-line {
  padding: 0 10px;
}
.product-line .titles {
  font-size: 14px;
  padding-bottom: 8px;
  margin: 15px 0 8px;
  font-weight: 500;
}
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 15px;
}
.grid-content {
  border: 1px solid #e9e9e9;
  height: 40px;
  line-height: 40px;
  font-size: 13px;
  text-align: center;
  border-radius: 3px;
}
.grid-content.active {
  color: #fff;
  border: 1px solid #578cff;
  background: #578cff;
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}
.el-col .grid-content:hover {
  border: 1px solid #ccc;
}
.payment-description {
  padding: 10px;
  padding-top: 0;
  color: #666;
}
.payment-description .title1 {
  font-weight: 700;
  font-size: 16px;
  padding-bottom: 5px;
  padding-top: 15px;
  color: #333;
  line-height: 30px;
}
hr {
  border: solid #ddd;
  border-width: 1px 0 0;
  clear: both;
  height: 0;
  margin: 30px 0 10px;
}
.payment-description .content1 {
  padding-bottom: 10px;
}
.block {
  width: 100%;
  margin-top: 15px;
}
.block .block-hd {
  font-size: 14px;
  font-weight: bold;
  line-height: 30px;
  margin: 0;
  padding: 5px 0 4px 0;
  text-transform: uppercase;
  border-bottom: 1px solid #e0e0e0;
}
.sub-block {
  padding: 20px;
}
ul.no-bullet {
  list-style: none;
  margin: 0;
  margin-bottom: 20px;
  padding: 0;
}
.sub-block .sub-line li {
  padding-bottom: 8px;
  color: #999;
}
.sub-block .sub-line .res {
  float: right;
  font-size: 14px;
  color: #333;
}
.sub-block .sub-total {
  border-top: 1px solid #e9e9e9;
  padding-top: 20px;
  padding-bottom: 30px;
  font-size: 15px;
}
.sub-block .sub-total .price-total {
  float: right;
  font-size: 28px;
  font-weight: 700;
}
.row {
  width: 100%;
  display: table;
}
.row .el-button {
  float: right;
}
</style>
  